package com.outputstream_;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author Vhong
 * @version 1.0
 */
public class FileOutputStream_ {
    public static void main(String[] args) {
        //创建FileOutputStream对象
        String filePath = "e:\\a.txt";
        FileOutputStream fileOutputStream = null;

        try {
            //得到FileOutputStream对象
            //1.new FileOutputStream(filePath)创建方式，当写入内容，会覆盖原来的内容
            //2.new FileOutputStream(filePath,true)创建方式，当写入内容是，是追加到文件后面
//            fileOutputStream = new FileOutputStream(filePath);
            fileOutputStream = new FileOutputStream(filePath,true);
            //写入一个字节
//          fileOutputStream.write('H');
            //写入字符串
            String str = "hello,world";
            //str.getBytes()可以把 字符串 -> 字节数组

            fileOutputStream.write(str.getBytes());
            /*
            public void write(byte[] b, int off, int len)将len字节从位于偏移量off的指定字节数组
             */
            fileOutputStream.write(str.getBytes(),0,3);


        } catch (IOException e) {
            e.printStackTrace();

        }finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}
